﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

using ECS.Framework;
using ECS.DAL;
using ECS.DAL.Entity;
using ECS.DAL.Component;

namespace E_ICS.E_ICS
{
    public partial class frmEditMAWBInfo : Form
    {
        #region private properties
        //string appId = ConfigurationManager.AppSettings["appID"];
        //string device = ConfigurationManager.AppSettings["device"];
        //SqlConnection sqlCon = null; 
        //DataSet datasetUpdate = null;
        //DateTime flight_date_std;
        private Main.frmMain2 main;
        private MAWBComponent _dataComponent = null;
        #endregion

        #region form events

        /// <summary>
        /// constructor form
        /// assign main form to internal variable
        /// </summary>
        /// <param name="_main"></param>
        public frmEditMAWBInfo(Main.frmMain2 _main)
        {
            InitializeComponent();
            main = _main;

        }
        /// <summary>
        /// show weight dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWeight_Click(object sender, EventArgs e)
        {
            try
            {
                E_ICS.frmEditSelectWeightId swid = new frmEditSelectWeightId();
                swid.Show();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        /// <summary>
        /// show dimension dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDim_Click(object sender, EventArgs e)
        {
            try
            {
                E_ICS.frmEditDim ed = new E_ICS.frmEditDim();
                ed.Show();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        /// <summary>
        /// show comment dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOtherInfo_Click(object sender, EventArgs e)
        {
            try
            {
                E_ICS.d eoi = new E_ICS.d(main);
                eoi.Show();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        /// <summary>
        /// close form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// load data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmEditMAWBInfo_Load(object sender, EventArgs e)
        {
            try
            {
                //sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["con3"].ConnectionString);
                _dataComponent = new MAWBComponent();
                SetScnfrmEditMAWBInfo();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAgentSearch_Click(object sender, EventArgs e)
        {
            try
            {
                ViewEditWeightSlip.flagfromViewEditWeightSlip = 1;

                E_ICS.frmAgentSearch a = new E_ICS.frmAgentSearch(main);
                if (a.ShowDialog() == DialogResult.Yes)
                {
                    GetDataFromAgentSearch();
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
            
        }
        /// <summary>
        /// Save MAWB
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateMAWB();
                this.DialogResult = DialogResult.Yes;
                this.Close();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
            
        }
        #endregion

        #region private methods

        /// <summary>
        /// assign data to controls
        /// </summary>
        private void SetScnfrmEditMAWBInfo()
        {
            txtCarrier.Text = ViewEditWeightSlip.Carrier;
            txtNO.Text = ViewEditWeightSlip.Flight_No;
            //     txtFlightDate.Text = ViewEditWeightSlip.Flight_Date;
            txtDest.Text = ViewEditWeightSlip.Destination;
            txtAgentCode.Text = ViewEditWeightSlip.Agent;
            txtAgentName.Text = ViewEditWeightSlip.Agent_Name;
            txtNature.Text = ViewEditWeightSlip.Nature_of_Goods;
            txtPieces.Text = ViewEditWeightSlip.Piece;
            dpkFlightDate.Value = _dataComponent.GetFlightDate(ViewEditWeightSlip.MAWB_ID); // flight_date_std;
        }
        /// <summary>
        /// 
        /// </summary>
        public void GetDataFromAgentSearch()
        {
            txtAgentCode.Text = ViewEditWeightSlip.Agent;
            txtAgentName.Text = ViewEditWeightSlip.Agent_Name;

        }
        /// <summary>
        /// update mawb
        /// </summary>
        private void UpdateMAWB()
        {
            MAWB data = new MAWB();
            try
            {
                data.AppId = GlobalVariable.user.ApplicationID;
                data.Device = GlobalVariable.user.Device;
                data.UserId = GlobalVariable.user.UserID;

                data.MawbId = ViewEditWeightSlip.MAWB_ID;
                data.Mawb = ViewEditWeightSlip.MAWB;
                data.MawbPrefix = ViewEditWeightSlip.MAWB.Substring(0, 3);
                data.MawbSerial = ViewEditWeightSlip.MAWB.Substring(4);
                if (Utilities.IsNumeric(ViewEditWeightSlip.awb_seq))
                {
                    data.AWBSequence = Convert.ToInt32(ViewEditWeightSlip.awb_seq);
                }
                if (Utilities.IsNumeric(ViewEditWeightSlip.awb_move_seq))
                {
                    data.AWBMoveSequence = Convert.ToInt32(ViewEditWeightSlip.awb_move_seq);
                }
                //data.PartialAcceptFlag = ViewEditWeightSlip.awb_move_seq;
                data.Hawb = ViewEditWeightSlip.HAWB;
                if (Utilities.IsNumeric(ViewEditWeightSlip.partial_seq))
                {
                    data.PartialSequence = Convert.ToInt32(ViewEditWeightSlip.partial_seq);
                }
                data.Carrier = txtCarrier.Text;
                data.FlightNumber = txtNO.Text;
                data.Destination = txtDest.Text;
                data.AgentCode = txtAgentCode.Text;
                data.AgentName = txtAgentName.Text;
                data.NoG = txtNature.Text;
                data.PieceCount = Convert.ToInt32(txtPieces.Text);
                data.FlightDate = dpkFlightDate.Value;
                _dataComponent.Update(data);
                if (_dataComponent.ErrorMessage.Status != "PASS")
                {
                    CommonMessageBox.ShowMessageWarning(_dataComponent.ErrorMessage.Message);
                }
                //SqlCommand cmd = new SqlCommand();
                //cmd.CommandText = "usp_UpdateMAWBRecord";
                //cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Connection = sqlCon;
                //cmd.Parameters.AddWithValue("@in_vchApplicationID", GlobalVariable.user.ApplicationID);
                //cmd.Parameters.AddWithValue("@in_vchMAWBID", ViewEditWeightSlip.MAWB_ID);
                //cmd.Parameters.AddWithValue("@in_vchMAWB", ViewEditWeightSlip.MAWB);
                //string mawb_prefix = ViewEditWeightSlip.MAWB.Substring(0, 3);
                //string mawb_sn = ViewEditWeightSlip.MAWB.Substring(4);
                //cmd.Parameters.AddWithValue("@in_vchMAWBPrefix", mawb_prefix);
                //cmd.Parameters.AddWithValue("@in_vchMAWBSerial", mawb_sn);
                //cmd.Parameters.AddWithValue("@in_intAWBSeq", ViewEditWeightSlip.awb_seq);
                //cmd.Parameters.AddWithValue("@in_intAWBMoveSeq", ViewEditWeightSlip.awb_move_seq);
                //cmd.Parameters.AddWithValue("@in_vchHAWB", ViewEditWeightSlip.awb_seq);
                //cmd.Parameters.AddWithValue("@in_intMAWBPartialSeq", ViewEditWeightSlip.partial_seq);
                //cmd.Parameters.AddWithValue("@in_vchDevice", GlobalVariable.user.Device);
                //cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.user.UserID);

                ////ส่วนที่ edit ได้จริงๆ
                //cmd.Parameters.AddWithValue("@in_vchCarrier", txtCarrier.Text);
                //cmd.Parameters.AddWithValue("@in_vchFlightNumber", txtNO.Text);
                //cmd.Parameters.AddWithValue("@in_vchDestination", txtDest.Text);
                //cmd.Parameters.AddWithValue("@in_vchAgentCode", txtAgentCode.Text);
                //cmd.Parameters.AddWithValue("@in_vchAgentName", txtAgentName.Text);
                //cmd.Parameters.AddWithValue("@in_vchNatureOfGoods", txtNature.Text);
                //cmd.Parameters.AddWithValue("@in_intPieceCount", txtPieces.Text);
                //cmd.Parameters.AddWithValue("@in_dtFlightDate", dpkFlightDate.Value);


                //sqlCon.Open();
                //SqlDataAdapter da = new SqlDataAdapter(cmd);
                //datasetUpdate = new DataSet();
                //da.Fill(datasetUpdate, "InsertDimensionRecord");
                //string status = datasetUpdate.Tables[0].Rows[0]["Status"].ToString();
                //string Messagest = datasetUpdate.Tables[0].Rows[0]["Message"].ToString();
                //if (status != "PASS")
                //{
                //    CommonMessageBox.ShowMessageWarning(Messagest);
                //}
            }
            catch (Exception objEx)
            {
                CommonMessageBox.ShowMessageError(objEx);
            }
            finally
            {
                //if (sqlCon != null && sqlCon.State == ConnectionState.Open)
                //{
                //    sqlCon.Close();
                //}
            }
        }

        //public void getFlightDate()
        //{


        //    SqlCommand cmd = new SqlCommand("select flight_date_std from t_eics_mawb where mawb_id=@mawb_id", sqlCon);

        //    SqlDataAdapter daversion = new SqlDataAdapter(cmd);
        //    cmd.Parameters.AddWithValue("@mawb_id", ViewEditWeightSlip.MAWB_ID);
        //    sqlCon.Open();
        //    SqlDataReader reader2 = cmd.ExecuteReader();

        //    if (reader2.Read())
        //    {
        //        flight_date_std = (DateTime)reader2["flight_date_std"];

        //    }
        //    sqlCon.Close();

        //}
        #endregion
    }
}
